﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Help;
using Model;

namespace BLLA
{
    public partial class View_Order_Shelves : BaseBLL<Model.View_Order_Shelves>, IBLL.IView_Order_ShelvesBLL
    {
        ObjectSerialize os = new ObjectSerialize();
        DataResult result = new DataResult();
        IBLL.IBLLSession bl = DI.SpringHelper.GetObject<IBLL.IBLLSession>("BLLSession");

        /// <summary>
        /// 根据商户ID获取该商户的所有上架信息列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="MerID">商户ID</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式（1：JSON；2：XML）</param>
        /// <returns></returns>
        public string GetOrder_Shelves(int pageIndex,int pageSize,string MerID, string YZM, int RETID)
        {
            try
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion
                List<Model.View_Order_Shelves> ls = new List<Model.View_Order_Shelves>();
                ls = base.GetListBy(u => u.MerID == MerID & u.SheState == 1);
                if (ls != null & ls.Count > 0)
                {
                    List<View_Order_Shelves_More> list = new List<View_Order_Shelves_More>();
                    foreach (Model.View_Order_Shelves item in ls)
                    {
                        View_Order_Shelves_More info = new View_Order_Shelves_More();
                        string[] dishes_ls = item.DishesID.Split(',');
                        string[] dishesnub_ls = item.DishesNub.Split(',');
                        List<Order_Dishes_T_More> list_dishes = new List<Order_Dishes_T_More>();
                        int i = 0;
                        foreach (string dishes_item in dishes_ls)
                        {
                            List<Model.View_Order_Dishes> dishes_ls_tmp = bl.IView_Order_DishesBLL.GetListBy(u => u.DishesID == dishes_item);
                            if (dishes_ls_tmp != null && dishes_ls_tmp.Count > 0)
                            {
                                Model.View_Order_Dishes dishes_tmp_info = dishes_ls_tmp.First();
                                Order_Dishes_T_More dishes_info = new Order_Dishes_T_More();
                                if (dishes_tmp_info.DishesEnableInt != null)
                                {
                                    dishes_info.DishesEnableInt = dishes_tmp_info.DishesEnableInt;
                                }
                                if (dishes_tmp_info.DishesEvaNun != null)
                                {
                                    dishes_info.DishesEvaNun = dishes_tmp_info.DishesEvaNun;
                                }
                                if (dishes_tmp_info.DishesEvaTxt != null)
                                {
                                    dishes_info.DishesEvaTxt = dishes_tmp_info.DishesEvaTxt;
                                }
                                if (dishes_tmp_info.DishesEvaX != null)
                                {
                                    dishes_info.DishesEvaX = dishes_tmp_info.DishesEvaX;
                                }
                                if (dishes_tmp_info.DishesGood != null)
                                {
                                    dishes_info.DishesGood = dishes_tmp_info.DishesGood;
                                }
                                if (dishes_tmp_info.DishesID != null)
                                {
                                    dishes_info.DishesID = dishes_tmp_info.DishesID;
                                }
                                if (dishes_tmp_info.DishesIsInt != null)
                                {
                                    dishes_info.DishesIsInt = dishes_tmp_info.DishesIsInt;
                                }
                                if (dishes_tmp_info.DishesMon != null)
                                {
                                    dishes_info.DishesMon = dishes_tmp_info.DishesMon;
                                }
                                if (dishes_tmp_info.DishesNmae != null)
                                {
                                    dishes_info.DishesNmae = dishes_tmp_info.DishesNmae;
                                }
                                if (dishes_tmp_info.DishesPay != null)
                                {
                                    dishes_info.DishesPay = dishes_tmp_info.DishesPay;
                                }
                                if (dishes_tmp_info.DishesPic != null)
                                {
                                    dishes_info.DishesPic = dishes_tmp_info.DishesPic;
                                }
                                if (dishes_tmp_info.DishesTxt != null)
                                {
                                    dishes_info.DishesTxt = dishes_tmp_info.DishesTxt;
                                }
                                if (dishes_tmp_info.DishesTypeID != null)
                                {
                                    dishes_info.DishesTypeID = dishes_tmp_info.DishesTypeID;
                                }
                                if (dishes_tmp_info.DishesTypeState != null)
                                {
                                    dishes_info.DishesTypeState = dishes_tmp_info.DishesTypeState;
                                }
                                if (dishes_tmp_info.DishesVol != null)
                                {
                                    dishes_info.DishesVol = dishes_tmp_info.DishesVol;
                                }
                                if (dishes_tmp_info.IntBZ != null)
                                {
                                    dishes_info.IntBZ = dishes_tmp_info.IntBZ;
                                }
                                if (dishes_tmp_info.MerID != null)
                                {
                                    dishes_info.MerID = dishes_tmp_info.MerID;
                                }
                                if (dishes_tmp_info.DishesTypeName != null)
                                {
                                    dishes_info.DishesTypeName = dishes_tmp_info.DishesTypeName;
                                }

                                dishes_info.DishesNub = Int32.Parse(dishesnub_ls[i++]);

                                list_dishes.Add(dishes_info);
                            }
                        }

                        info.DisheList = list_dishes;

                        if (item.BZ != null)
                        {
                            info.BZ = item.BZ;
                        }
                        if (item.DishesID != null)
                        {
                            info.DishesID = item.DishesID;
                        }
                        if (item.DishesNub != null)
                        {
                            info.DishesNub = item.DishesNub;
                        }
                        if (item.MaDay != null)
                        {
                            info.MaDay = item.MaDay;
                        }
                        if (item.MaTime != null)
                        {
                            info.MaTime = item.MaTime;
                        }
                        if (item.MerID != null)
                        {
                            info.MerID = item.MerID;
                        }
                        if (item.MerName != null)
                        {
                            info.MerName = item.MerName;
                        }
                        if (item.SheDateB != null)
                        {
                            info.SheDateB = item.SheDateB;
                        }
                        if (item.SheDateE != null)
                        {
                            info.SheDateE = item.SheDateE;
                        }
                        if (item.SheID != null)
                        {
                            info.SheID = item.SheID;
                        }
                        if (item.SheName != null)
                        {
                            info.SheName = item.SheName;
                        }
                        if (item.SheState != null)
                        {
                            info.SheState = item.SheState;
                        }
                        if (item.SheTimeB != null)
                        {
                            info.SheTimeB = item.SheTimeB;
                        }
                        if (item.SheTimeE != null)
                        {
                            info.SheTimeE = item.SheTimeE;
                        }
                        list.Add(info);
                    }

                    result.BS = "1";
                    result.Msg = "查询成功";
                    result.rows = list;
                    result.rows = list.Skip((pageIndex - 1) * pageSize).Take(pageSize);
                    result.total = list.Count;
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }
                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("根据商户ID获取该商户的所有上架信息列表失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";               
                return os.StringOfObject(result, RETID);
            }
        }

        public class Order_Dishes_T_More : Model.View_Order_Dishes
        {
            public int DishesNub { get; set; }
        }

        public class View_Order_Shelves_More : Model.View_Order_Shelves
        {
            public List<Order_Dishes_T_More> DisheList { get; set; }
        }
    }
}
